package com.xunfang.demo.servlet3;

import com.xunfang.demo.service.UserService;
import com.xunfang.demo.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "DelServlet", urlPatterns = "/del")
public class DelServlet extends HttpServlet {

    UserService userService = new UserServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //如何接收get请求传递的数据？
        String id = request.getParameter("id");
        boolean bool = userService.removeUserById(Integer.valueOf(id));
        //重定向到MainServlet，再次查询用户列表
        response.sendRedirect(request.getContextPath()+"/main");
    }
}
